package net.hvacapp.cmvrfforandroid.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import net.hvacapp.cmvrfforandroid.R;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据库对象连接基类 负责对数据库连接的管理和创建
 * <p>
 * 文件名: DatabaseHelper.java
 * </p>
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static DatabaseHelper databaseHelper;
    private static final String DATABASE_NAME = "talk.db";

    //	private static final int DATABASE_VERSION = 1;
    private static final int DATABASE_VERSION = 4;//数据库升级1-2

    private static Map<String, DatabaseHelper> dbconns = new HashMap<String, DatabaseHelper>();

    public static DatabaseHelper getInstance(Context context) {
        if (databaseHelper == null) {
            databaseHelper = new DatabaseHelper(context);
        }
        return databaseHelper;
    }

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION,
                R.raw.ormlite_config);
    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        Class<?>[] classes = DatabaseConfigUtil.classes;
        try {
            for (Class<?> clazz : classes) {
                TableUtils.createTable(databaseHelper.getConnectionSource(),
                        clazz);
            }
            initDatabase(db);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("创建数据库失败");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
                          int oldVersion, int newVersion) {

        if (oldVersion == newVersion) {
            return;
        }

        Class<?>[] classes = DatabaseConfigUtil.classes;
        try {
            int updateVersion = oldVersion;

//            if (updateVersion == 1) {// 1-->2 添加DBCache表
//                TableUtils.createTable(connectionSource, DBCache.class);
//                TableUtils.createTable(connectionSource, Cache.class);
//                updateVersion = 2;
//            }
//
//            if (updateVersion == 2) {// 2-->3 添加UserPassword表
//                TableUtils.createTable(connectionSource, UserPassword.class);
//                updateVersion = 3;
//            }
//
//            if (updateVersion == 3) {// 2-->3 添加UserPassword表
//                TableUtils.createTable(connectionSource, DBLog.class);
//                updateVersion = 4;
//            }

            // 异常情况，先删表后添加表
            if (updateVersion != newVersion) {
                for (Class<?> clazz : classes) {
                    TableUtils.dropTable(databaseHelper.getConnectionSource(),
                            clazz, true);
                }
                for (Class<?> clazz : classes) {
                    TableUtils.createTable(
                            databaseHelper.getConnectionSource(), clazz);
                }
                initDatabase(db);
            }
        } catch (Exception e) {
            logger.error("更新数据库失败:{}", e.getMessage());
        }
    }

    // 初始化数据
    public void initDatabase(SQLiteDatabase db) {

    }

    public static void release(String key) {
        if (dbconns.containsKey(key)) {
            dbconns.remove(key);
        }
    }

}
